<?php

namespace App\Listener;

use Lib\Core\ConfigLoad;
use Lib\Log\LogHelper;
use Swoole\Coroutine\Http\Client;

/**
 * @Listener(ws.start)
 * @description:
 * @auth:yfl
 * @email:554665488@qq.com
 * Class OpenListener
 * @package App\Listener
 */
class StartListener
{
    /**
     * @description:服务启动注册到服务中心
     * @auth:yfl
     * @email:554665488@qq.com
     * @param array $params
     */
    public function handel(array $params)
    {
//        LogHelper::Write('服务启动注册到服务中心');
//        var_dump('StartListener触发');
        $config = ConfigLoad::getInstance();
        $config->load();//载入配置文件
        $wsCenterConfig = $config::get('ws_center');
        go(function () use ($wsCenterConfig) {
            $cli = new Client($wsCenterConfig['host'], $wsCenterConfig['port']);
            $ret = $cli->upgrade("/");
            if ($ret) {
                $data = [
                    'method' => 'register',
                    'service' => 'IM_1',
                    'ip' => '106.14.120.16',
                    'port' => 9503
                ];
                $cli->push(json_encode($data));
//                var_dump($cli->recv());
                swoole_timer_tick(3000, function () use ($cli) {
                    if ($cli->errCode == 0) {
                        $cli->push('', WEBSOCKET_OPCODE_PING);
                    }
                });

            }
        });
    }
} 